package renguanyu.app.codebuilder.view;

import java.util.Set;
import java.util.TreeSet;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.MenuButton;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
import renguanyu.app.codebuilder.model.Template;
import renguanyu.app.codebuilder.util.DaoFactory;


public class CreateCodeView extends AnchorPane{

    Label templateLabel = new Label("请选择一个模板");
    MenuButton templateListMenu = new MenuButton();
    ComboBox<Template> templateListCombo = new ComboBox();
    Label createWayLabel = new Label("请选择一个生成方式");
    RadioButton outDiskRadio = new RadioButton("生成到硬盘");
    RadioButton outViewRadio = new RadioButton("生成到页面");
    Button createCodeButton = new Button("生成代码");
    Label codeFormatLabel = new Label("请输入代码格式");
    TextField codeFormatField = new TextField();

//    ScrollPane scrollPane = new ScrollPane();
//    AnchorPane paramPane = new AnchorPane();

    public CreateCodeView(){
        initUI(); 
        initEvent();
        initData();
    }

    private void initUI() {

        AnchorPane.setLeftAnchor(templateLabel,10.0);
        AnchorPane.setTopAnchor(templateLabel,10.0);
        AnchorPane.setRightAnchor(templateLabel,400.0);

        AnchorPane.setLeftAnchor(templateListCombo,200.0);
        AnchorPane.setTopAnchor(templateListCombo,10.0);
        AnchorPane.setRightAnchor(templateListCombo,10.0);

        this.getChildren().addAll(templateLabel, templateListCombo);

        AnchorPane.setLeftAnchor(createWayLabel,10.0);
        AnchorPane.setTopAnchor(createWayLabel,50.0);
        AnchorPane.setRightAnchor(createWayLabel,400.0);

        ToggleGroup schemeGroup = new ToggleGroup();
        outDiskRadio.setToggleGroup(schemeGroup);
        AnchorPane.setLeftAnchor(outDiskRadio,200.0);
        AnchorPane.setTopAnchor(outDiskRadio,50.0);
        AnchorPane.setRightAnchor(outDiskRadio,307.0);

        outViewRadio.setToggleGroup(schemeGroup);
        outViewRadio.setSelected(true);
        AnchorPane.setLeftAnchor(outViewRadio,350.0);
        AnchorPane.setTopAnchor(outViewRadio,50.0);

        this.getChildren().addAll(createWayLabel, outDiskRadio, outViewRadio);

        AnchorPane.setLeftAnchor(codeFormatLabel,10.0);
        AnchorPane.setTopAnchor(codeFormatLabel,90.0);
        AnchorPane.setRightAnchor(codeFormatLabel,400.0);

        codeFormatField.setPromptText("例如:.java或者.html");
        AnchorPane.setLeftAnchor(codeFormatField,200.0);
        AnchorPane.setTopAnchor(codeFormatField,90.0);
        AnchorPane.setRightAnchor(codeFormatField,10.0);

        this.getChildren().addAll(codeFormatLabel, codeFormatField);

        this.setPrefHeight(180.0);
        this.setPrefWidth(200.0);

    }

    private void initEvent() {
        templateListCombo.setOnAction(event -> queryTempDataTo());
    }

    private void queryTempDataTo() {
        Template template = templateListCombo.getSelectionModel().getSelectedItem();
        String templateId = template.getId();
        ObservableList<ObservableMap<String, String>> templateData = DaoFactory.getTemplateDataDao().query(templateId);
        Set<String> elList = new TreeSet<>();
        for(int i=0;i<templateData.size();i++){
            ObservableMap<String, String> templateDataItem = templateData.get(i);
            String fData = templateDataItem.get("F_DATA");
//            elList.add(template);
        }
    }


    private void initData() {
        ObservableList<Template> tempList = FXCollections.observableArrayList();
        ObservableList<ObservableMap<String, String>> templateList = DaoFactory.getTemplateDao().query(null);

        for(int i=0;i<templateList.size();i++){

            ObservableMap<String, String> map = templateList.get(i);
            String tempId = map.get("TEMP_ID");
            String tempName = map.get("TEMP_NAME");

            Template template = new Template();
            template.setId(tempId);
            template.setName(tempName);
            templateListCombo.getItems().add(i,template);
        }

    }
}
